/*
Coded 5/11/24 - Questions to terence.wood@anu.edu.au

This runs the standard tests of potential issues in difference in difference
analysis for the paper "Does independent review reduce the reported performance 
of aid projects?" (The tests reported in section "5.3: addressing: potential 
issues"

Coded in Version 18.0 SE.
If you are using an old version of Stata you may need to install estout/esttab

First it tests for parallel trends.
Then it examines early closing projects.
*/


********************************Preliminaries***********************************
graph drop _all
estimates clear

// If you are running this section on its own change the Macros to 
// the appropriate files/locations on your computer.

cd "$data_loc"  // Change directory to location where data are stored.
use "$data_file", clear // Open data
cd "$res_loc" // Change directory to location where analysis will occur.

****************************Table 3 - Parallel Trends***************************
// Test for violation of parallel trends.

preserve
encode country, gen(cencode) // encode country to enable country fixed effects
local projectcontrols "duration lnbudget i.pacific" // controls used in some models

// Create a variable which starts at the first year of analysis.                                                    
gen date = year-2014
label var date "Year"

// Parallel trends over full time series
// Basic model
qui reg ee c.date##i.finalreport if date<5
estimates store simple

// Controls added
qui reg ee c.date##i.finalreport `projectcontrols' if date<5   
estimates store controls

// Sector and country Fixed effects added.
qui reg ee c.date##i.finalreport `projectcontrols' i.sector i.cencode if date<5   
estimates store controlsfx

// Now all regressions repeated but excluding 2016.
qui reg ee c.date##i.finalreport if date<5 & date!=2
estimates store ex2016simple

qui reg ee c.date##i.finalreport `projectcontrols' if date<5 & date!=2  
estimates store ex2016controls

qui reg ee c.date##i.finalreport `projectcontrols' i.sector i.cencode if date<5 & date!=2  
estimates store ex2016controlsfx

// All regressions exported to a table.
esttab simple controls controlsfx ex2016simple ex2016controls ex2016controlsfx ///
	using "4 Table 3 - Parallel trends.rtf", replace ///
	b(2) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
	nobase nogap nodep nomtitle noconst label varwidth(20) modelwidth(6) ///
	indicate("Project Controls = duration lnbudget *pacific" ///
	"Sector & Country FE = *sector *cencode") ///
	coeflabels(1.finalreport#c.date "Parallel trends issue?") ///
	order(1.finalreport#c.date) /// 
	note(Robust standard errors are clustered at the project level. ///
	The 'Parallel trends issue?' coefficient is the interaction of ///
	year and final report. If this coefficient is statistically ///
	significant it indicates a violation of the parallel trends ///
	assumption. Model 1 tests for parallel trends. Model 2 tests for ///
	parallel trends controlling for the project traits that are used in ///
	the main difference in difference regressions. Model 3 contains these ///
	controls as well as sector and country fixed effects. Models 4-6 repeat ///
	the tests used in models 1-3, but excluding 2016 because of the major ///
	aid cuts that occurred in that year.) fonttbl(\f0\fnil Calibri; )

// Tidy up.	
drop date
estimates clear
restore

********************Figure 2 - Projects closing early***************************
// Testing to see if worse performing projects were closed early in anticipation
// of the treatment commencing.

set graph off

// First chart: are early closers worse projects?
cibar ee if finalreport==1, over1(earlyclose) barcolor(gs6 gs12) ///
baropts(lcolor(gs6)) graphopts(name(projperf) ///
ytitle("Mean project performance") ylab(1(1)5) legend(position(6) ///
cols(2)) xlab(,nogrid)  title(Performance of projects closed early))

// Second chart: early closing projects over time.
graph bar (count) pnencode (sum) earlyclose if finalreport==1, over(year) name(numpro) ///
legend(label(1 "Not early") label(2 "Early") position(6) cols(2)) ///
ytitle("Projects completed") title(Number of projects closed early) ///
bar(1, fcolor(gs6) lcolor(gs6)) bar(2, fcolor(gs12) lcolor(gs6)) 

// Combine the two charts
graph combine projperf numpro, cols(2) graphregion(color(white))

set graph on

// Display the resulting chart.
graph display, ysize(6) xsize(12)
graph export "5 Figure 2 Projects closing early.jpg", width(10000) replace

// Tidy up
graph drop _all
estimates clear